package com.gadgets.cm01.role.service.impl;

import com.gadgets.cm01.role.dto.RoleOperDTO;
import com.gadgets.cm01.role.dto.RoleQueryDTO;
import com.gadgets.cm01.role.pojo.Role;
import com.gadgets.cm01.role.repository.IRoleMapper;
import com.gadgets.cm01.role.service.IRoleManagerService;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-10-10
 *
 */
@Service
@Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = {Throwable.class})
public class RoleManagerServiceImpl implements IRoleManagerService {

    @Autowired
    private IRoleMapper roleMapper;

    /**
     * 新增角色信息
     *
     * @param roleInfo 角色信息
     *
     * @return 新增数量
     *
     * @throws GadgetsServiceException 服务异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = {Throwable.class})
    @Override
    public int addRoleInfo(Role roleInfo) throws GadgetsServiceException {
        return roleMapper.insertRole(roleInfo);
    }

    /**
     * 更新角色信息
     *
     * @param roleInfo 角色信息
     *
     * @return 更新条数
     *
     * @throws GadgetsServiceException 服务异常
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = {Throwable.class})
    @Override
    public int editRoleInfo(RoleOperDTO roleInfo) throws GadgetsServiceException {
        return roleMapper.updateRole(roleInfo);
    }

    /**
     * 删除已废弃角色信息
     *
     * @param deleteId 删除记录ID
     *
     * @return 更新条数
     *
     * @throws GadgetsServiceException 服务异常
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = {Throwable.class})
    @Override
    public int deleteRoleInfo(Long[] deleteId) throws GadgetsServiceException {
        int i = 0;
        if(null!=deleteId && deleteId.length>0){
            for (Long id : deleteId) {
                i += deleteRoleInfo(id);
            }
        }
        return i;
    }

    /**
     * 删除已废弃角色信息
     *
     * @param deleteId 删除记录ID
     *
     * @return 更新条数
     *
     * @throws GadgetsServiceException 服务异常
     */
    protected int deleteRoleInfo(Long deleteId) throws GadgetsServiceException{
        return roleMapper.deleteRole(deleteId);
    }

    /**
     * 分页查询角色信息
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     *
     * @return 查询结果
     *
     * @throws GadgetsServiceException 服务异常
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = {Throwable.class})
    @Override
    public List<Role> queryRoleInfoByCondition(RoleQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<Role> roleList = roleMapper.selectRoleInfo(queryDTO);
        PaginationUtil.getPaginationResultInfo(roleList,pagination);
        return roleList;
    }

}
